﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;

namespace Web
{
    public partial class adminKnowledge : System.Web.UI.Page
    {
        private int SubjectID = 0, BookID = 0, editid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterKnow"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["subjectid"] == null)
            {
                Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
            }
            else
            {
                if (!Utility.chk_int(Request.QueryString["subjectid"]))
                {
                    Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
                }
                else
                {
                    SubjectID = Convert.ToInt32(Request.QueryString["subjectid"].Trim());
                }
            }
            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    KnowledgeInfo info = Knowledge.GetKnowledgeInfoById(editid);
                    KnowledgeName.Text = info.Name;
                    KnowledgeKeyword.Text = info.Keyword;
                }
                LoadList(0, "");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (KnowledgeName.Text.Trim() == "")
                {
                    Utility.ShowJsAlert("请填写知识点名称", this);
                }
                else if (KnowledgeKeyword.Text.Trim() == "")
                {
                    Utility.ShowJsAlert("请填写知识点关键字", this);
                }
                else
                {
                    int ParentID = 0;
                    if (KnowledgeList.SelectedValue.Length > 0)
                    {
                        ParentID = int.Parse(KnowledgeList.SelectedValue.ToString());
                    }
                    KnowledgeInfo knowledgeinfo = new KnowledgeInfo();

                    if (editid == 0)
                    {
                        knowledgeinfo.Parent = ParentID;
                        knowledgeinfo.Subject = SubjectID;
                        knowledgeinfo.Book = BookID;
                    }
                    else
                    {
                        knowledgeinfo = Knowledge.GetKnowledgeInfoById(editid);
                    }
                    knowledgeinfo.Name = KnowledgeName.Text.Trim();
                    knowledgeinfo.Keyword = KnowledgeKeyword.Text;
                    knowledgeinfo.Subject = SubjectID;
                    if (editid > 0)
                    {
                        if (Knowledge.CheckKnowledgeExistByKeyword(knowledgeinfo.Keyword, editid,SubjectID))
                        {
                            Utility.ShowJsAlert(knowledgeinfo.Keyword+"-"+editid.ToString(), this);
                        }
                        else
                        {
                            Knowledge.EditKnowledge(knowledgeinfo);
                            editid = 0;
                            Response.Redirect("adminKnowledge.aspx?subjectid=" + SubjectID + "&bookid=" + BookID);
                        }
                    }
                    else
                    {
                        if (Knowledge.CheckKnowledgeExistByKeyword(knowledgeinfo.Keyword,editid,SubjectID))
                        {
                            Utility.ShowJsAlert("知识点已存在，请勿重复添加", this);
                        }
                        else
                        {
                            Knowledge.AddKnowledge(knowledgeinfo);
                            KnowledgeKeyword.Text = "";
                            KnowledgeName.Text = "";
                            LoadList(0, "");
                        }
                    }
                }

            }
            catch
            {
                Utility.ShowJsAlert("保存失败", this);
            }
        }

        protected void LoadList(int ParentID,string line)
        {
            if (ParentID == 0)
            {
                KnowledgeList.Items.Clear();
                IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetRootKnowledgeInfo(BookID, SubjectID);
                foreach (KnowledgeInfo info in knowledgeInfos)
                {
                    ListItem item = new ListItem(info.Name, info.Id.ToString());
                    KnowledgeList.Items.Add(item);
                    LoadList(info.Id, "——");
                }
            }
            else
            {
                IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetKnowledgeInfos(ParentID);
                foreach (KnowledgeInfo info in knowledgeInfos)
                {
                    ListItem item = new ListItem(line+info.Name, info.Id.ToString());
                    KnowledgeList.Items.Add(item);
                    LoadList(info.Id, "——" + line);
                }
            }
        }

        protected void DelBut_Click(object sender, EventArgs e)
        {
            
            if (KnowledgeList.SelectedValue == "")
            {
                Utility.ShowJsAlert("请选择要删除的知识点", this);
            }
            else
            {
                int ID = int.Parse(KnowledgeList.SelectedValue.ToString());
                if (Knowledge.CheckKnowledgeExistSub(ID))
                {
                    Utility.ShowJsAlert("请先删除子知识点", this);
                }
                else
                {
                    Knowledge.DelKnowledge(ID);
                    LoadList(0, "");
                }
            }
        }
        protected void EditBut_Click(object sender, EventArgs e)
        {
            
            if (KnowledgeList.SelectedValue == "")
            {
                Utility.ShowJsAlert("请选择要编辑的知识点", this);
            }
            else
            {
                int ID = int.Parse(KnowledgeList.SelectedValue.ToString());
                Response.Redirect("adminKnowledge.aspx?subjectid=" + SubjectID + "&bookid=" + BookID + "&editid=" + ID);
            }
        }

    }
}
